<?php
class ManagersController extends AppController {
	var $helpers = array('Html', 'Form', 'Session','Ajax','Javascript');
	var $name = 'Managers';
	var $layout='manager';
	function admin_index() {
		$this->Manager->recursive = 0;
		$this->set('managers', $this->paginate());
	}

	function logout (){
		$this->redirect(array('controller' => 'accounts','action' => 'logout'));
	
	}
	
	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid manager', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('manager', $this->Manager->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Manager->create();
			if ($this->Manager->save($this->data)) {
				$this->Session->setFlash(__('The manager has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The manager could not be saved. Please, try again.', true));
			}
		}
		$accounts = $this->Manager->Account->find('list');
		$profiles = $this->Manager->Profile->find('list');
		$organizations = $this->Manager->Organization->find('list');
		$this->set(compact('accounts', 'profiles', 'organizations'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid manager', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Manager->save($this->data)) {
				$this->Session->setFlash(__('The manager has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The manager could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Manager->read(null, $id);
		}
		$accounts = $this->Manager->Account->find('list');
		$profiles = $this->Manager->Profile->find('list');
		$organizations = $this->Manager->Organization->find('list');
		$this->set(compact('accounts', 'profiles', 'organizations'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for manager', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Manager->delete($id)) {
			$this->Session->setFlash(__('Manager deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Manager was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
		
	function profile (){
		$id = 1;
		$manager = $this->Manager->findById($id);
		$this->set('managers', $manager);		
		if(!empty($this->data)){
			if($this->data['Manager']['sex']==0) $this->data['Manager']['sex']='male';
			else	$this->data['Manager']['sex']='female'; 	
			$this->data['Manager']['birthday']=$this->data['Manager']['birthday_array']['day']."/".
				$this->data['Manager']['birthday_array']['month']."/".$this->data['Manager']['birthday_array']['year'];
			unset($this->data['Manager']['birthday_array']);
			$this->loadModel('Profile');
			$this->data['Profile'] = $this->data['Manager'];
			$sql = "UPDATE profiles SET fullname = '".$this->data['Manager']['fullname']."',".
											"sex = '".$this->data['Manager']['sex']."',".
											"birthday = '".$this->data['Manager']['birthday']."',".
											"email = '".$this->data['Manager']['email']."',".
											"address = '".$this->data['Manager']['address']."',".
											"phone = '".$this->data['Manager']['phone']."' ".	
											"WHERE id = ".$manager['Profile']['id'];
			$this->Session->setFlash($sql);
			if ($this->Profile->query($sql)) {
				$this->Session->setFlash('Your profile has been updated.');
				$this->redirect(array('action' => 'index'));
        }
		}
		$this->set('tea', $this->data);
	}			
	/**
	 * @author Kien Bui
	 */
	function index (){
		$this->redirect('home');
	}
	function home (){
		$id=1;
		$manager = $this->Manager->findById($id);
		$this->set('managers', $manager);
		$this->loadModel('Account');
		foreach ($manager['Teacher'] as $i){
			$teacher = $this->Account->findById($i['account_id']);
			$teachers[] = $teacher;
		}
		$this->set('teachers', $teachers);
	}
		
	function delete_teacher($id1,$id2) {
		$this->loadModel('Account');
		$this->loadModel('Teacher');		
	    if ($this->Account->delete($id1)&&$this->Teacher->delete($id2)) {
			$this->Session->setFlash('A account with id: ' . $id2 . ' has been deleted.');
			$this->redirect(array('action' => 'home'));
		}
	}
	
	function list_teacher (){
		$this->set ('action','List_Teacher');
	}
	function add_teacher (){
		$id=1;
		$manager = $this->Manager->findById($id);
		$this->set('managers', $manager);
		$this->loadModel('Account');
		$this->loadModel('Teacher');
        if (!empty($this->data)) {
			//$this->set('data1', $this->data);			
			$this->data['Manager']['password']=sha1($this->data['Manager']['username'].$this->data['Manager']['password']);
			$this->data['Manager']['role'] = 'teacher';
			$this->data['Manager']['status'] = 0;
			$this->data['Manager']['last_login'] = "0000-00-00 00:00:00";
			$this->data['Account'] = $this->data['Manager'];
			if($this->Account->save($this->data)){
				$s = $this->Account->find('first', array('conditions' => array('Account.username' => $this->data['Manager']['username'])));	
				$this->data['Teacher']['account_id'] = $s['Account']['id'];
				$this->data['Teacher']['manager_id'] = $id;
				$this->data['Teacher']['profile_id'] = rand(1000,9999);
				$this->data['Teacher']['last_login'] = "0000-00-00 00:00:00";
				//$this->data['Manager'][] = array('role' => 'teacher','status' => '0');
				if ($this->Teacher->save($this->data)) {
					$this->Session->setFlash('New account has been created.');
					$this->redirect(array('action' => 'index'));
				}
				else{
					$this->Session->setFlash('New account has not been created.');
					//       delete ban ghi Account
				}
			}	
        }
    }
	
	function change_password (){
		$id=1;
		$manager = $this->Manager->findById($id);
		$this->set('managers', $manager);
		//Security::hash($this->data['User']['password'])
		//$this->Auth->password('itj@manager');
		if (!empty($this->data)){
			if($manager['Account']['password'] ==  $this->Auth->password($this->data['Manager']['Old Password'])){
				if($this->data['Manager']['New Password'] == $this->data['Manager']['Retype New Password']){
					$this->loadModel('Account');
					$sql = "UPDATE accounts SET password = '".$this->Auth->password($this->data['Manager']['New Password'])."' ".
													"WHERE id = ".$manager['Account']['id'];
					//$this->Session->setFlash($sql);
					if ($this->Account->query($sql)) {
		                $this->Session->setFlash('New password has been changed.');
						$this->redirect(array('action' => 'index'));	
					}
				}
				else 	$this->Session->setFlash('Retype New Password be not match.');
			}
			else	$this->Session->setFlash('Old Password be not match.');
		}
	}
	
	function view_teacher ($id=null){
		$id1=1;
		$manager = $this->Manager->findById($id1);
		$this->set('managers', $manager);
		$this->loadModel('Account');
		$account = $this->Account->findById($id);
		$this->set('account', $account);		
	}
	
	function search_teacher (){
		if(!empty($this->data))	{
			if($this->data['Search']['keyword']!=''){
				$this->layout = 'ajax'; //su dung layout ajax thay cho layout mac dinh
				//$this->autoRender = false;
				$keyword = $this->data['Search']['keyword'];
				$this->loadModel('Account');			
				$accountLists = $this->Account->find('all', array(
					'conditions' => array(
						'Account.username like "%'.mysql_escape_string($keyword).'%"'
					)
				));			
				$this->set('accountLists', $accountLists);	
			}	
			else{
				$this->layout = 'ajax';
				$id=1;
				$manager = $this->Manager->findById($id);
				$this->set('managers', $manager);
				$this->loadModel('Account');
				foreach ($manager['Teacher'] as $i){
					$teacher = $this->Account->findById($i['account_id']);
					$teachers[] = $teacher;
				}
				$this->set('accountLists', $teachers);
			}
		}
	}  
}
